package handler

import (
	"context"
	"user-server/basic/dao"
	"user-server/basic/model"
	pb "user-server/proto"
)

type UserServer struct {
	pb.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *UserServer) Register(_ context.Context, in *pb.RegisterReq) (*pb.RegisterResp, error) {
	user := model.User{
		Tel:      in.Tel,
		Password: in.Password,
	}
	dao.Create(&user)
	return &pb.RegisterResp{
		Msg:  "注册成功",
		Code: 200,
	}, nil
}
func (s *UserServer) Login(_ context.Context, in *pb.LoginReq) (*pb.LoginResp, error) {
	var user model.User
	dao.Find3(in.Tel, &user)
	if user.Id == 0 {
		return &pb.LoginResp{
			Msg:  "用户不存在",
			Code: 500,
		}, nil
	}
	dao.FInd2(in.Tel, in.Password, &user)
	if user.Id == 0 {
		return &pb.LoginResp{
			Msg:  "手机号和密码不正确",
			Code: 500,
		}, nil
	}
	return &pb.LoginResp{
		Msg:  "登录成功",
		Code: 200,
	}, nil
}
func (s *UserServer) List(_ context.Context, in *pb.ListReq) (*pb.ListResp, error) {
	var user []model.User
	dao.Find(&user)
	var l []*pb.Item
	for _, m := range user {
		ls := pb.Item{
			Tel:      m.Tel,
			Password: m.Password,
			Id:       int64(m.Id),
		}
		l = append(l, &ls)
	}
	return &pb.ListResp{
		List: l,
	}, nil
}
